<template>
  <ContentWrap>
    <el-alert class="!mb-[10px]" :title="t('mentorProfile.edit.tips')" type="error" />
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
      label-position="top"
    >
      <el-form-item :label="t('mentorProfile.fields.avatar')" prop="avatar">
        <EditImage v-model="formData.avatar" />
      </el-form-item>
      <el-form-item :label="t('mentorProfile.fields.phone')" prop="phone">
        <el-input
          v-model="formData.phone"
          :placeholder="t('common.inputTextPlaceholder') + t('mentorProfile.fields.phone')"
        />
      </el-form-item>
      <el-form-item :label="t('mentorProfile.fields.email')" prop="email">
        <el-input
          v-model="formData.email"
          :placeholder="t('common.inputTextPlaceholder') + t('mentorProfile.fields.email')"
        />
      </el-form-item>
      <el-form-item :label="t('mentorProfile.fields.research')" prop="research">
        <Editor v-model="formData.research" height="350px" class="w1/1" />
      </el-form-item>
      <el-form-item :label="t('mentorProfile.fields.profile')" prop="profile">
        <Editor v-model="formData.profile" height="350px" class="w1/1" />
      </el-form-item>
    </el-form>
    <el-affix position="bottom" :offset="10">
      <el-button @click="submitForm" type="primary" :disabled="formLoading">
        {{ t('common.save') }}
      </el-button>
    </el-affix>
  </ContentWrap>
</template>

<script setup lang="ts">
import { MentorProfileApi, MentorProfileVO } from '@/api/recruit/mentorprofile'
import { FormRules } from 'element-plus'
import EditImage from '@/views/core/ustcstudent/components/input/EditImage.vue'
import { Editor } from '@/components/Editor'

/** 个人招生简介 表单 */
defineOptions({ name: 'MentorProfileEdit' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  mentorId: undefined,
  avatar: '',
  phone: undefined,
  email: undefined,
  research: '',
  profile: '',
  remark: undefined
})
const formRules = reactive<FormRules>({
  // avatar: [
  //   {
  //     required: true,
  //     message: t('common.selectTextPlaceholder') + t('mentorProfile.fields.avatar'),
  //     trigger: 'change'
  //   }
  // ],
  phone: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('mentorProfile.fields.phone'),
      trigger: 'blur'
    }
  ],
  email: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('mentorProfile.fields.email'),
      trigger: 'blur'
    }
  ],
  research: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('mentorProfile.fields.research'),
      trigger: 'blur'
    }
  ],
  profile: [
    {
      required: true,
      message: t('common.inputTextPlaceholder') + t('mentorProfile.fields.profile'),
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const getData = async () => {
  formLoading.value = true
  try {
    formData.value = await MentorProfileApi.getMyMentorProfile()
  } finally {
    formLoading.value = false
  }
}

/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  try {
    await formRef.value.validate()
  } catch {
    return
  }
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as Partial<MentorProfileVO>
    await MentorProfileApi.saveMyMentorProfile(data)
    message.success(t('common.updateSuccess'))
  } finally {
    formLoading.value = false
  }
}

onMounted(() => {
  getData()
})
</script>
